################################################################################ 
#
# Replication code for:
# Empathy-based counterspeech can reduce racist hate speech
# in a social media field experiment
#
# PNAS 2021
# 
# 
# Additional Unpublished Analysis
# --------------------------------
# 
# We replicate our main analysis after recoding two outcome variables:
# 'num_hate_tweets_post', 'num_tweets_post'.
# In particular, we recode as 0 their values for attrited users. 
# The interpretation of these variables then should change to: number of public tweets, 
# and number of public hate tweets.
# We show that our results are robust to recoding.
# 
################################################################################ 


rm(list = ls())
setsave = F

################################################################################ 
#  LIBRARIES
################################################################################ 

library(stargazer)
library(dplyr)
library(tidyr)
library(readr)
library(ggplot2)
library(estimatr)
library(xtable)

################################################################################ 
#   DATA AND FOLDER
################################################################################ 

wd = dirname(dirname(dirname(rstudioapi::getActiveDocumentContext()$path)))
wd_res = paste0(wd, '/results')
wd_data =  paste0(wd, '/data')

setwd(wd_data)

# Upload the data
data = read.csv('main_dataset.csv')

# Recode outcomes
data$num_hate_tweets_post[data$deleted_suspended==1] = 0
data$num_tweets_post[data$deleted_suspended==1] = 0

################################################################################ 
#   VARIABLES
################################################################################

data$empathy_dummy = ifelse(data$treatment_main==3, 1, 0)
data$consequences_dummy = ifelse(data$treatment_main==2, 1, 0)
data$meme_dummy = ifelse(data$treatment_main==1, 1, 0)

data$treated = ifelse(data$ITT==0 & data$treatment_dummy==1, 1, 0)

################################################################################ 
#   Prepare
################################################################################ 

outvars = c('num_hate_tweets_post', 'num_tweets_post')

################################################################################ 
#  OLS REGRESSIONS with recoded outcomes
################################################################################ 

container_meme = list()
for (j in 1:length(outvars)){
  print(paste0(outvars[j], '----------------------------'))
  Yfull = data[, outvars[j]] %>% scale()
  Y = Yfull[data$treatment_main %in% c(0,1)] 
  mod = lm_robust(Y ~ treatment_dummy, data=data[data$treatment_main %in% c(0,1),])
  container_meme[[j]] = mod
}

container_cons = list()
for (j in 1:length(outvars)){
  print(paste0(outvars[j], '----------------------------'))
  Yfull = data[, outvars[j]] %>% scale()
  Y = Yfull[data$treatment_main %in% c(0,2)] 
  mod = lm_robust(Y ~ treatment_dummy, data=data[data$treatment_main %in% c(0,2),])
  container_cons[[j]] = mod
}

container_empathy = list()
for (j in 1:length(outvars)){
  print(paste0(outvars[j], '----------------------------'))
  Yfull = data[, outvars[j]] %>% scale()
  Y = Yfull[data$treatment_main %in% c(0,3)] 
  mod = lm_robust(Y ~ treatment_dummy, data=data[data$treatment_main %in% c(0,3),])
  container_empathy[[j]] = mod
}


# Tables 
out1 = sapply(container_meme, function(x) summary(x)$coefficients[2, c(1,2,4)]) %>% xtable(digits=c(3))
out2 = sapply(container_cons, function(x) summary(x)$coefficients[2, c(1,2,4)]) %>% xtable(digits=c(3))
out3 = sapply(container_empathy, function(x) summary(x)$coefficients[2, c(1,2,4)]) %>% xtable(digits=c(3))





################################################################################ 
#  OLS REGRESSIONS with original outcomes
################################################################################ 

data = data[is.na(data$deleted_suspended),]

container_meme = list()
for (j in 1:length(outvars)){
  print(paste0(outvars[j], '----------------------------'))
  Yfull = data[, outvars[j]] %>% scale()
  Y = Yfull[data$treatment_main %in% c(0,1)] 
  mod = lm_robust(Y ~ treatment_dummy, data=data[data$treatment_main %in% c(0,1),])
  container_meme[[j]] = mod
}

container_cons = list()
for (j in 1:length(outvars)){
  print(paste0(outvars[j], '----------------------------'))
  Yfull = data[, outvars[j]] %>% scale()
  Y = Yfull[data$treatment_main %in% c(0,2)] 
  mod = lm_robust(Y ~ treatment_dummy, data=data[data$treatment_main %in% c(0,2),])
  container_cons[[j]] = mod
}

container_empathy = list()
for (j in 1:length(outvars)){
  print(paste0(outvars[j], '----------------------------'))
  Yfull = data[, outvars[j]] %>% scale()
  Y = Yfull[data$treatment_main %in% c(0,3)] 
  mod = lm_robust(Y ~ treatment_dummy, data=data[data$treatment_main %in% c(0,3),])
  container_empathy[[j]] = mod
}


# Tables 
out1 = sapply(container_meme, function(x) summary(x)$coefficients[2, c(1,2,4)]) %>% xtable(digits=c(3))
out2 = sapply(container_cons, function(x) summary(x)$coefficients[2, c(1,2,4)]) %>% xtable(digits=c(3))
out3 = sapply(container_empathy, function(x) summary(x)$coefficients[2, c(1,2,4)]) %>% xtable(digits=c(3))
